package com.aeye.mbr.ws.api;

import com.aeye.mbr.common.base.BaseResult;
import com.aeye.mbr.common.base.constants.ErrorCodeConstants;
import com.aeye.mbr.common.exception.ServiceException;

import java.io.Serializable;


public class BaseResp<T extends Serializable> implements Serializable{
	public static final int SUCCESS = 0;
	private int respCode;
	
	private String respMsg;
	
	private T respData;

	public int getRespCode() {
		return respCode;
	}

	public void setRespCode(int respCode) {
		this.respCode = respCode;
	}

	public String getRespMsg() {
		return respMsg;
	}

	public void setRespMsg(String respMsg) {
		this.respMsg = respMsg;
	}

	public T getRespData() {
		return respData;
	}

	public BaseResp setRespData(T respData) {
		this.respData = respData;
		return this;
	}

	public static BaseResp success()
	{
		BaseResp result = new BaseResp();
		result.setRespCode(ErrorCodeConstants.SUCCESS.getVal());
		result.setRespMsg(ErrorCodeConstants.SUCCESS.getDesc());
		return result;
	}
	
	public static BaseResp success(String message)
	{
		BaseResp result = new BaseResp();
		result.setRespCode(ErrorCodeConstants.SUCCESS.getVal());
		result.setRespMsg(message);
		return result;
	}
	
	public static BaseResp error(ErrorCodeConstants ec)
	{
		BaseResp result = new BaseResp();
		result.setRespCode(ec.getVal());
		result.setRespMsg(ec.getDesc());
		return result;
	}
	
	public static BaseResp error(Exception e)
	{
		BaseResp result = new BaseResp();
		result.setRespCode(-99);
		result.setRespMsg("处理异常:" + e.getMessage());
		return result;
	}
	
	public static BaseResp error(BaseResult br)
	{
		BaseResp result = new BaseResp();
		result.setRespCode(br.getCode());
		result.setRespMsg(br.getMessage());
		return result;
	}
	
	public static BaseResp error(ServiceException e)
	{
		BaseResp result = new BaseResp();
		result.setRespCode(e.getCode());
		result.setRespMsg(e.getMessage());
		return result;
	}
	
	public boolean isSuccess()
	{
		return respCode == SUCCESS;
	}
}
